package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/*
 * 登录功能
 */
public class LoginDemo {
    public static void main(String[] args) {
        //1.注册驱动
        //2.获取连接
        //3.获取执行对象
        //4.执行SQL语句
        //5.处理结果
        //6.释放资源

        try(Connection connection=DBUtil.getConnection();){
            Statement statement=connection.createStatement();
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入用户名：  用户密码:");
            String username=scanner.nextLine();
            String password=scanner.nextLine();
            String sql="select username,password " +
                    "FROM user " +
                    "WHERE username='"+username+"' and password='"+password+"'";
            System.out.println(sql);
            ResultSet num=statement.executeQuery(sql);
//            while (num.next()){
//                String usename1= num.getString("username");
//                String password1= num.getString("password");
//                if(usename1.equals(username)&&password1.equals(password)){
//                    System.out.println("登录成功");
//                }else{
//                    System.out.println("登录失败");
//                }
//            }
            if (num.next()){
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败");
            }



        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
